Questions
31 of 38
1What is the Iterator Protocol in JavaScript? What two things must an object implement to be a valid iterator?
2What is the difference between an iterable and an iterator? Can something be both?
3What built-in JavaScript data structures are iterable by default? How does for...of work under the hood with them?
4What does Symbol.iterator do? How would you make a plain object iterable from scratch?
5What is a 'lazy' iterator and why is it important for performance? Give a practical example.
6How would you implement an infinite iterator (e.g., an infinite counter)? How do you safely consume it?
7What happens if you forget to return { done: true } in a custom iterator? What are the consequences?
8How would you implement a reusable iterable (one that can be iterated multiple times)?
9Can you chain or compose custom iterators? Implement a map and filter for a custom iterator without converting to an array.
10What is a generator function? How is it different from a regular function in terms of execution flow?
11Explain what yield does. What does calling .next() return before and after a yield?
12What is the difference between yield and return inside a generator?
13What does yield* do? How is it different from a regular yield?
14Can you pass a value into a generator using .next(value)? How does that work and what is the practical use case?
15Explain generator.return(value) and generator.throw(error). When would you use each in production?
16What happens to a try/finally block inside a generator when .return() is called externally?
17How do generators handle errors? How does .throw() interact with try/catch inside a generator?
18Are generators lazy? How do they differ from eager evaluation with arrays in terms of memory and performance?
19What is the difference between a regular iterator and an async iterator? What protocol does an async iterator follow?
20What does Symbol.asyncIterator do? How does for await...of use it?
21Implement an async generator that fetches paginated API data, yielding one page at a time:
22What are the pitfalls of using for await...of with an async generator that makes network calls? How do you handle errors and cancellation?
23How would you implement a readable stream as an async iterable in Node.js?
24How would you use a generator to implement redux-saga-style side effect management? What makes generators a good fit for this?
25How can generators be used to implement coroutines or cooperative multitasking in JavaScript?
26Implement a take(n) utility that takes the first n values from any iterable — including infinite ones:
27When would you choose a generator over returning an array? What are the memory trade-offs?
28In a data pipeline processing millions of records, how would you use generators to avoid loading everything into memory?
29What are the debugging challenges with generators (e.g., in stack traces and async flows)? How do you mitigate them?
30How do generators compose? Implement a pipeline of generator-based transformations (like RxJS operators but synchronous).
31What are the limitations of generators? What problems are they not a good fit for?
32What are Iterator Helpers (.map(), .filter(), .take(), .drop() on iterators natively)? What stage are they at in the TC39 proposal pipeline?
33How does Array.from() use the iterator protocol? What's the difference between passing an iterable vs an array-like object?
34How do Map, Set, Array, and String expose their iterators? Are they the same object or different?
35What is the difference between map.keys(), map.values(), and map.entries()? What do they return?
36How does destructuring and spread (...) use the iterator protocol internally?
37How would you implement Promise-based async/await using generators and a runner function? (This is essentially how Babel transpiled async/await early on.)
38How would you use a generator to implement a tree traversal (DFS) without recursion stack concerns?
31 / 38

What are the limitations of generators? What problems are they not a good fit for?

Generators cannot be reused (once exhausted, they're done). They are not ideal for random access or multiple-pass algorithms. They also cannot be used for async operations without a runner.

Generators are single-use; you cannot restart them. They lack random access (no [index]). They are not suitable for algorithms that require multiple passes over the data unless you regenerate the generator. They also do not natively support concurrency or parallelism; you need additional machinery (like a scheduler) for that.

Difficulty: 5/10
Topics: lazy evaluation, stateful iteration, concurrency limits

Scenario Questions

0-2 years experience
  1. 1

    How would you use a generator to read lines from a large file without loading the whole file into memory? What happens if you try to iterate twice over the same generator instance?

  2. 2

    If you call .next() on a generator after it has already returned {done:true}, what does JavaScript return?

  3. 3

    Write a simple generator that yields the first N Fibonacci numbers. What limitation would you hit if you needed to restart the sequence without creating a new generator?

2-5 years experience
  1. 1

    We have a function that streams API responses using a generator, but it started throwing errors when network latency increased. Why might a plain generator be a poor fit for this scenario, and how would you modify the implementation?

  2. 2

    During a code review, a teammate replaced a for‑loop with a generator to improve readability, yet performance regressed. What aspects of generators could cause this slowdown?

  3. 3

    Explain why using a generator to implement a cache that needs random access to previously yielded items is problematic.

5-8 years experience
  1. 1

    Design a data‑processing pipeline that handles millions of records. Would you base it on generators, async iterators, or another approach? Discuss trade‑offs regarding back‑pressure, error propagation, and resource usage.

  2. 2

    Our microservice uses a generator to produce events for downstream consumers, but we now need multiple consumers to read the same stream concurrently. What limitations of generators affect this, and how would you redesign the component?

  3. 3

    When profiling a Node.js service, you notice a generator‑based iterator causing high CPU usage due to repeated context switches. Explain why generators may not scale well under high concurrency and propose an alternative.

8+ years experience
  1. 1

    A legacy codebase heavily relies on synchronous generators for data ingestion, and we now need to migrate to a distributed streaming platform like Kafka. What architectural challenges arise from the generator model, and how would you plan the migration?

  2. 2

    Across several teams, generators are being used as a substitute for async streams, leading to inconsistent error handling and back‑pressure semantics. As a staff engineer, how would you establish guidelines or refactor the system to address these issues?

  3. 3

    Consider a long‑running server that keeps a generator open for hours to serve client‑side pagination. What risks does this pose for memory leaks and state consistency, and how would you redesign the API to be more robust at scale?

Follow-up Questions

  • What tooling or metrics would you use to spot generator‑related performance issues in production?
  • If you had to refactor a generator‑based API to support multiple consumers, what pattern would you choose?
  • Can you give an example where the lazy nature of a generator could introduce a subtle bug?